<?php
declare (strict_types = 1);

namespace app\model;
use think\Model;

/**
 * @mixin \think\Model
 */
class Comments extends Model
{
    /**
     * 编写评论
     * @param $id
     * @param $data
     * @return int|string
     */
    public static function comment($id,$data)
    {

       self::insert($data);
      return  self::getLastInsID();
    }

    /**
     * 评论列表
     * @param $id
     * @param $limit
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public static function getList($id,$limit)
    {
      return  self::where('product_id',$id)
           ->where('status',1)
            ->with(['user'=>function($query){
                $query->field('username,head_pic,id');
            },'product'=>function($query){
                $query->field('id,name,cover_pic,price');
            }])
            ->order('id','desc')
            ->hidden(['users_id','pid','order_id'])
            ->paginate($limit);
    }

    public function user()
    {
        return $this->hasOne(Users::class,'id','users_id');
    }

    public function product()
    {
        return $this->hasOne(Product::class,'id','product_id');
    }
}
